package com.atguigu.juc.chap10;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureDemo05 {

    public static void main(String[] args) {

        // 1、创建线程池对象
        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        // 2、获取连续返回的结果
        CompletableFuture.supplyAsync(() -> {
                    System.out.println(Thread.currentThread().getName() + "：1");
                    return "prev task result";
                }, threadPool)

                // 封装后续任务，并接收前面任务的返回值
                .thenApply((String prevTaskResult) -> {
                    System.out.println(Thread.currentThread().getName() + "： 2 prevTaskResult = " + prevTaskResult);

                    // 自己有返回值
                    return "current task result";
                }).whenComplete((String prevTaskResult, Throwable throwable) -> {
                    System.out.println(Thread.currentThread().getName() + "： 3 prevTaskResult = " + prevTaskResult);
                });

        System.out.println("finish");

        // 3、关闭线程池
        threadPool.shutdown();
    }
}